extends Node2D

# 摄像机属性
@export var ZOOM_SPEED = 20.0
@export var ZOOM_MARGIN = 0.1
@export var ZOOM_MIN = 0.5
@export var ZOOM_MAX = 3.5
@export var POS_MIN = Vector2(160, 80)
@export var POS_MAX = Vector2(46, 25)

# 摄像机
@onready var camera = $Camera2D
# 提示文字
@onready var tip_label = $Camera2D/Label

signal click_node_back
signal show_child_back

var node_data: Array = []
var node_prefab_list: Array = []

# 节点预制体
var node_prefab: PackedScene

# 拖拽状态
var dragging = false
# 是否允许拖拽
var is_dragg = false
# 是否允许缩放
var is_zoom = true
# 摄像机属性
var zoomFacter = 1.0
var zoomPos = Vector2()
var zooming = false

var sel_node_panel = null


# Called when the node enters the scene tree for the first time.
func _ready():
	node_data = FileManage.get_arr_data("res://Datas/kaichang.csv")
	# 输出所有数据
	#print(node_data)
	node_prefab = load("res://Bases/node_base.tscn")
	set_scene_node()
	camera.position = Vector2(650, 355)
	
	# 接收点击节点回调信号
	click_node_back.connect(_click_node_back_connect)
	# 接收显示子节点回调信号
	show_child_back.connect(_show_child_back_connect)


func set_scene_node():
	for data in node_data:
		if data['type'] == "1":
			var node_info = node_prefab.instantiate()
			node_info.name += data['id']
			add_child(node_info)
			node_prefab_list.push_back(node_info)
			node_info.set_def_data(data, self, null)
			node_info.set_pos(Vector2(600, 340))

func _click_node_back_connect(panel):
	for n_prefab in node_prefab_list:
		if n_prefab != panel:
			n_prefab.set_def_type()
	sel_node_panel = panel
	if panel.click_type == 'short':
		if sel_node_panel.node_type == 'def':
			sel_node_panel.set_sel_type()
		elif sel_node_panel.node_type == 'sel':
			sel_node_panel.set_look_type()
			tip_label.text = sel_node_panel.node_data['text']
	elif panel.click_type == 'long':
		is_dragg = false
	elif panel.click_type == 'double':
		if sel_node_panel.node_type == 'look' or sel_node_panel.node_type == 'sel':
			sel_node_panel.show_child()

func _show_child_back_connect(panel):
	for c_node in panel.child_list:
		var node_info = node_prefab.instantiate()
		var n_name = node_info.name + str(c_node)
		var is_have = false
		var c_data = {}
		for v_node in node_prefab_list:
			if v_node.name == n_name:
				is_have = true
		for n_data in node_data:
			if n_data['id'] == c_node:
				c_data = n_data
		if not is_have:
			node_info.name = n_name
			add_child(node_info)
			node_prefab_list.push_back(node_info)
			node_info.set_def_data(c_data, self, panel)
			var s_keys = panel.child_panel_dic.keys()
			var c_null = null
			var c_null_type = ""
			for s_key in s_keys:
				if not panel.child_panel_dic[s_key].data and not c_null:
					c_null = panel.child_panel_dic[s_key]
					c_null_type = s_key
			if c_null:
				node_info.set_pos(c_null.pos)
				panel.set_child_data(c_null_type, c_data, node_info)

func _input(event):
	if event is InputEventMouseButton:
		if event.button_index == MOUSE_BUTTON_NONE:
			return
		zooming = false
		# 拖拽摄像机显示
		if event.button_index == MOUSE_BUTTON_LEFT:
			dragging = event.pressed
			$Timer.start()
			if not event.pressed:
				is_dragg = false
		## 放大摄像机显示
		#if event.button_index == MOUSE_BUTTON_WHEEL_DOWN and event.is_pressed() and is_zoom:
			#zooming = true
			#zoomFacter -= 0.01 * ZOOM_SPEED
		## 缩小摄像机显示
		#if event.button_index == MOUSE_BUTTON_WHEEL_UP and event.is_pressed() and is_zoom:
			#zooming = true
			#zoomFacter += 0.01 * ZOOM_SPEED
	
	# 拖拽移动摄像机
	if dragging and is_dragg:
		if event is InputEventMouseMotion:
			camera.position -= event.relative
			#if camera.position.x < 0:
				#camera.position.x = 0
			#if camera.position.y < 0:
				#camera.position.y = 0
			#camera.position.x = clamp(
				#camera.position.x, 
				#POS_MIN.x, 
				#container.custom_minimum_size.x - POS_MAX.x * camera.zoom.x
				#)
			#camera.position.y = clamp(
				#camera.position.y,
				#POS_MIN.y,
				#container.custom_minimum_size.y - POS_MAX.y * camera.zoom.y
				#)


func _on_timer_timeout():
	is_dragg = true
